/**
* @package Scribble
*/
$(document).ready(function() {

	// Prep all "external" hyperlinks
	$('a[rel*=external]').click(function() {
		window.open(this.href);
		return false;
	});

	// Prepare the help dialog modal window
	$('#scribble-help-dialog').dialog({

		/* Options */
		autoOpen: false,
		height: 400,
		modal: true,
		resizable: false,
		width: 700,

		/* Event handlers */
		dragStart: function() {
			$(this).children('iframe').hide();
		},
		dragStop: function() {
			$(this).children('iframe').show();
		}
	}).html('<iframe style="border:none; width:100%; height:350px;"></iframe>');

	// Prep all "help" hyperlinks.
	// These will load up the help content in the floating dialog box (as defined
	// above)
	$('a[rel*=help]').addClass('help');
	$('a[rel*=help]').click(function() {
		$('#scribble-help-dialog iframe').attr('src', this.href);
		$('#scribble-help-dialog').dialog('open');
		return false;
	});

	// Highlight the current main menu
	var activeMenu = $('#nav-main a[href='+self.location.pathname+']').addClass('active');
	if(activeMenu.size()==0) {
		var urlSuffix = Buan.Config.get('ext.Scribble.urlSuffix')
		var reUrlSuffix = new RegExp("^(.+)"+urlSuffix.replace(/\./, '\\.')+"$");
		var pathname = self.location.pathname.replace(reUrlSuffix, "$1").replace(/\/[^\/]+$/, urlSuffix).replace(/\/+$/, '');
		while(pathname!=urlSuffix) {
			activeMenu = $('#nav-main a[href='+pathname+']').addClass('active');
			if(activeMenu.size()>0) {
				break;
			}
			pathname = pathname.replace(/\/[^\/]+$/, urlSuffix).replace(/\/+$/, '');
		}
	}

	// If the highlighted menu has submenus, expand them so they're permanently
	// visible. This is done by pulling the submenu out of it's parent container
	// and displaying it below the main menu.
	if(activeMenu.size()>0) {
		var submenu = $(activeMenu.parent('li').children('ul'));
		if(submenu.size()==0) {
			var openMenu = $(activeMenu.parent('li').parent('ul').parent('li').children('a')).addClass('open');
			submenu = $(openMenu.parent('li').children('ul'));
		}
		submenu.hide().addClass('submenu').appendTo($('#nav-main')).fadeIn();
	}

	// Convert panels within "std-tabbed" blocks to a tabbed UI
	$('.std-tabbed').each(function(i, el) {

		// Build a group of tabs using the <h3> tag in each "std" panel as the tab
		// label. If any text within the <h3> tag is underlined (ie. <u>...</u>)
		// then that text will be taken and used as the tab's "accesskey"
		var tabs = $('<ol class="tabs"></ol>');
		$(el).find('.std').each(function(stdIndex, stdEl) {
			var anchor = 't'+i+'-'+stdIndex;
			var h3 = $(stdEl).find('h3');
			var label = h3.size()>0 ? $(h3).remove().html() : 'Tab #'+stdIndex;
			var m;
			var accesskey = (m = label.match(/<u>([^<]+)<\/u>/i)) ? m[1].toLowerCase() : null;
			var tab = $('<li><a href="#'+anchor+'"'+(accesskey===null ? '' : ' accesskey="'+accesskey+'"')+'></a></li>');
			tab.find('a').html(label).click(function() {
				$(this).parents('.std-tabbed').find('.std').css('display', 'none');
				$('#'+this.href.replace(/^.+#(.+)$/, "$1")).css('display', 'block');
				$(this).parents('li').siblings('li').find('a').removeClass('active');
				$(this).addClass('active');
				return false;
			});;
			tabs.append(tab);
			$(stdEl).attr('id', anchor);

			// Activate this tab
			if(stdIndex==0) {
				tab.find('a').addClass('active');
				$(stdEl).css('display', 'block');
			}
		});
		$(el).prepend(tabs);
	});
});